# -*- coding: UTF-8 -*-
# @Time : 2021/8/11 0011 9:29
# @Author : lyw
# @Remark : 自定义异常
import json

from mxsoftpy.exception import MxBaseException


class VerifyError(MxBaseException):
    """
    验证异常
    """

    def __str__(self):
        if not self.msg:
            msg = ''
        else:
            msg = self.msg

        return json.dumps({'status': 'failed', 'errmsg': msg}, ensure_ascii=False)


class ConnectionURLError(MxBaseException):
    """
    连接url异常
    """

    def __str__(self):
        if not self.msg:
            msg = '连接url错误'
        else:
            msg = '%s: 连接url错误' % self.msg

        return json.dumps({'status': 'failed', 'errmsg': msg}, ensure_ascii=False)


class MethodExecError(MxBaseException):
    """
    方法执行异常
    """

    def __init__(self, err_func: str, msg: str = None):
        """
        :param err_func: 异常的方法
        :param msg: 错误提示信息
        """
        super().__init__()
        self.err_func = err_func
        self.msg = msg

    def __str__(self):
        if not self.msg:
            msg = '方法执行错误，出错方法：%s' % self.err_func
        else:
            msg = '方法执行错误，出错方法：%s，错误原因：%s' % (self.err_func, self.msg)

        return json.dumps({'status': 'failed', 'errmsg': msg}, ensure_ascii=False)
